﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace AuctionHelper.Core
{
    /// <summary>
    /// 窗体辅助类
    /// </summary>
    public class WindowHelper
    {
        /// <summary>
        /// 查找窗口
        /// </summary>
        /// <param name="lpClassName"></param>
        /// <param name="lpWindowName"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        protected static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        
        /// <summary>
        /// 将最小化窗口还原
        /// </summary>
        /// <param name="hWnd"></param>
        /// <param name="nCmdShow"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        protected static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        /// <summary>
        /// 将指定窗口激活
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        [DllImport("user32.dll ")]
        protected static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>
        /// 窗体句柄ID
        /// </summary>
        public static IntPtr Window;

        /// <summary>
        /// 最大化窗口
        /// </summary>
        protected const int MaxSize = 3;

        /// <summary>
        /// 获取窗体
        /// </summary>
        /// <param name="windowName">窗体标题</param>
        /// <returns></returns>
        public static bool GetWindow(string windowName)
        {
            Window = FindWindow(null, windowName);
            return Window != IntPtr.Zero;
        }

        /// <summary>
        /// 激活窗体
        /// </summary>
        public static void ActiveWindow()
        {
            ShowWindow(Window, MaxSize);
            SetForegroundWindow(Window);
        }

    }
}
